<?php


namespace app\admin\model;


use app\admin\service\MenuService;

class MenuModel extends BaseAdminModel
{
    protected $table = "blog_admin_menu";

    /**
     * @param array $param 校验通过的参数
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function searchMenu(array $param) {
        $menu_name = $param['menu_name'] ?? null;
        $menu_path = $param['menu_path'] ?? null;
        $condition = [
            'translatedName'=>['menu_name', 'like', "%$menu_name%"],
            'movieName'=>['menu_path', 'like', "%$menu_path%"],
        ];
        return $this->toPaginate($param, $condition);
    }

    /**
     * 根据id更新对应的状态
     * @param int $id
     * @param int $status
     * @return boolean
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function updateStatusById(int $id, int $status) {
        $model = $this->find($id);
        if(!$model){
            return false;
        }
        // 需要更新的数据
        $arr = [
            ['id' => $id, 'status'=>$status],
        ];
        $server = new MenuService();
        $menuList = $this->select()->toArray();
        if(count($menuList)!==0){
            // 获取到对应ID下面的所以子孙
            $lists = $server->categoryMenuByParentId($menuList, $id);
            $updateList = $server->updateStatusList($lists, $status);
            foreach ($updateList as $item) {
                array_push($arr, $item);
            }
        }
        $bool = $this->saveAll($arr);
        if($bool){
            return true;
        }
        return false;
    }
}